package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.AppVersionDao;
import com.baichang.taoshunda.entity.AppVersion;
import com.baichang.taoshunda.service.AppVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AppVersionServiceImpl implements AppVersionService {

    @Autowired
    private AppVersionDao appVersionDao;


    @Override
    public Page<Map<String, Object>> queryAllApp(int id, int nowPage, int pageSize) throws ServiceException {
        return appVersionDao.queryAppPage(id, nowPage, pageSize);
    }

    @Override
    public boolean editById(int id, int version, String tagversion, String url, String updateInfo, int isForce) throws ServiceException {
        if (id == 0) {
            throw new ServiceException("编号错误");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("version", version+1);
        map.put("tagVersion", tagversion);
        map.put("url", url);
        map.put("updateInfo", updateInfo);
        map.put("updated",new Date());
        map.put("isForce", isForce);
        if (appVersionDao.update(map, id) > 0) {
            return true;
        }
        return false;
    }


    /**
     * 进行删除
     *
     * @throws ServiceException
     */
    @Override
    public boolean delete(int id) throws ServiceException {
        return appVersionDao.delete(id) > 0;
    }

    /**
     * 检查版本更新
     *
     * @param version  版本
     * @param platform 平台
     * @return
     */
    @Override
    public Map<String, Object> checkAppVersion(Integer version, Integer platform) {
        Map<String, Object> map = new HashMap<String, Object>();
        AppVersion appVersion = appVersionDao.getAppVersionByPlatform(platform);
        if (appVersion.getVersion() > version) {
            map = appVersion.toMap();
        }
        return map;
    }
}
